// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Xrm.Sdk
{
	[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification = "Nullable arguments cause issues for compiler.")]
	[AttributeUsage(AttributeTargets.Property)]
	public sealed class RelationshipSchemaNameAttribute : Attribute
	{
		/// <summary>
		/// Gets or sets the CRM schema name of the relationship.
		/// </summary>
		public string SchemaName
		{
			get { return _relationship.SchemaName; }
		}

		/// <summary>
		/// Gets or sets the name of the <see cref="EntityRole"/> of the relationship.
		/// </summary>
		public EntityRole? PrimaryEntityRole
		{
			get { return _relationship.PrimaryEntityRole; }
		}

		private readonly Relationship _relationship;

		/// <summary>
		/// Gets the <see cref="Relationship"/> of the attribute.
		/// </summary>
		internal Relationship Relationship
		{
			get { return _relationship; }
		}

		public RelationshipSchemaNameAttribute(string schemaName) :
			this(schemaName, null)
		{
		}

		public RelationshipSchemaNameAttribute(string schemaName, EntityRole primaryEntityRole) :
			this(schemaName, (EntityRole?)primaryEntityRole)
		{
		}

		private RelationshipSchemaNameAttribute(string schemaName, EntityRole? primaryEntityRole)
		{
			if (string.IsNullOrWhiteSpace(schemaName)) throw new ArgumentNullException("schemaName");

			_relationship = new Relationship(schemaName);
			_relationship.PrimaryEntityRole = primaryEntityRole;
		}
	}
}
